package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.ChannelEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IChannelService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: UnionPayCenter
 * @description: 管理员
 * @author: bite
 * @create:
 **/
@Slf4j
@Api(value = "channel", tags = "渠道")
@RestController
@RequestMapping("/channel")
public class ChannelController {
    @Autowired
    private IChannelService channelService;

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ApiOperation(tags = "渠道", httpMethod = "POST", value = "[后台]查询渠道列表", response = ChannelEntity.class)
    public ResponseWrapper findList(
            @ApiJsonObject(name = "channelFindListParams", value = {
                @ApiJsonProperty(key = "token", value = "token值"),
                @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                @ApiJsonProperty(key = "name", value = "条件", example = "线上注册", required = false),
            }) @RequestBody JSONObject jsonParam) {
        try {
            List<ChannelEntity> list = channelService.findList(jsonParam);
            return ResponseWrapper.succeed(list);
        }catch (ApiException e){
            log.error(String.format("[后台]查询渠道列表失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询渠道列表失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(tags = "渠道", httpMethod = "POST", value = "[后台]渠道添加", response = String.class)
    public ResponseWrapper add(
            @ApiJsonObject(name = "channelAddParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "name", value = "渠道名称", example = "线上注册"),
                    @ApiJsonProperty(key = "shortName", value = "渠道简称", example = "inline")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String name = jsonParam.getString("name");
            if(StringUtils.isNullStr(name)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "name"));
            }
            String shortName = jsonParam.getString("shortName");
            if(StringUtils.isNullStr(shortName)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "shortName"));
            }
            channelService.add(jsonParam);
            return ResponseWrapper.succeed("添加成功");
        }catch (ApiException e){
            log.error(String.format("[后台]添加渠道失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]添加渠道失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(tags = "渠道", httpMethod = "POST", value = "[后台]渠道修改", response = String.class)
    public ResponseWrapper update(
            @ApiJsonObject(name = "channelUpdateParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "channelId", value = "渠道id", type = "int"),
                    @ApiJsonProperty(key = "name", value = "渠道名称", example = "线上注册"),
                    @ApiJsonProperty(key = "shortName", value = "渠道简称", example = "inline")
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer managerId = jsonParam.getInteger("managerId");
            if (managerId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW100);
            }
            Integer channelId = jsonParam.getInteger("channelId");
            if (channelId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "channelId"));
            }
            String name = jsonParam.getString("name");
            if(StringUtils.isNullStr(name)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "name"));
            }
            String shortName = jsonParam.getString("shortName");
            if(StringUtils.isNullStr(shortName)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "shortName"));
            }
            channelService.update(jsonParam);
            return ResponseWrapper.succeed("修改成功");
        }catch (ApiException e){
            log.error(String.format("[后台]修改渠道失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]修改渠道失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
